"use strict";
cc._RF.push(module, '7fa8fcvrqFOj6lhh6xHzd3c', 'MJRoom');
// scripts/components/MJRoom.js

"use strict";

var _timers = require("timers");

cc.Class({
    extends: cc.Component,

    properties: {
        gameRoot: {
            default: null,
            type: cc.Node
        },

        // prepareRoot: {
        //     default: null,
        //     type: cc.Node
        // },

        _myMJArr: [],
        _options: null,
        _selectedMJ: null,
        _chupaiSprite: [],
        _mjcount: null,
        // _gamecount: null,
        _hupaiTips: [],
        _hupaiLists: [],
        _playEfxs: [],
        _opts: [],
        // _seats: [],
        // _timeLabel: null,
        // _voiceMsgQueue: [],
        // _lastPlayingSeat: null,
        // _playingSeat: null,
        // _lastPlayTime: null,
        _folds: null,
        _btnKZ: null,
        _zhishaibtn: null,
        _chisArr: []
        // _cur: null,
        // _seatsResult: []
    },

    onLoad: function onLoad() {
        if (!cc.sys.isNative && cc.sys.isMobile) {
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }
        if (!cc.vv) {
            cc.director.loadScene("loading");
            return;
        }
        this.addComponent("PengGangs");
        this.addComponent("UserInfoShow");

        this.addComponent("TimePointer");
        this.addComponent("Common");
        // this.addComponent("ReplayCtrl");
        this.addComponent("Chat");
        this.addComponent("PopupMgr");
        this.addComponent("ReConnect");
        this.addComponent("Voice");
        this.addComponent("GameResult");

        this.gameRoot.active = false;
        cc.vv.audioMgr.playBGM("bgFight.mp3");

        this.initView();
        cc.vv.common.initSeats();
        this.initEventHandlers();
        this.initAllFolds();
    },

    initView: function initView() {
        this._btnKZ = cc.find("Canvas/arrow/btnKZ");
        // if (!cc.vv.gameNetMgr.gameState && !cc.vv.replayMgr.isReplay()) {
        //     if (cc.vv.gameNetMgr.buttonid == cc.vv.userMgr.userId) {
        //         this._btnKZ.active = true;
        //     }
        // }
        //搜索需要的子节点
        var gameChild = this.node.getChildByName("game");
        var seats = this.node.getChildByName("seats");
        this._mjcount = cc.find("Canvas/arrow/mjcount").getComponent(cc.Label);
        this._mjcount.string = "剩余" + cc.vv.gameNetMgr.numOfMJ + "张";
        this._zhishaibtn = cc.find("Canvas/arrow/zhishai_sp");

        var myselfChild = gameChild.getChildByName("myself");
        var myholds = myselfChild.getChildByName("holds");

        for (var i = 0; i < myholds.children.length; ++i) {
            var sprite = myholds.children[i].getComponent(cc.Sprite);
            this._myMJArr.push(sprite);
            sprite.spriteFrame = null;
        }

        var realwidth = cc.director.getVisibleSize().width;
        myholds.scaleX *= realwidth / 1280;
        myholds.scaleY *= realwidth / 1280;

        var sides = ["myself", "right", "up", "left"];
        // this._cur = [];
        this._folds = {};
        for (var i = 0; i < sides.length; ++i) {
            var side = sides[i];
            var sideChild = gameChild.getChildByName(side);
            this._hupaiTips.push(sideChild.getChildByName("HuPai"));
            this._playEfxs.push(sideChild.getChildByName("play_efx").getComponent(cc.Animation));
            this._chupaiSprite.push(sideChild.getChildByName("ChuPai").children[0].getComponent(cc.Sprite));
            var folds = [];
            var foldRoot = sideChild.getChildByName("folds");
            var cur = sideChild.getChildByName("cur");
            for (var j = 0; j < foldRoot.children.length; ++j) {
                var n = foldRoot.children[j];
                n.active = false;
                var sprite = n.getComponent(cc.Sprite);
                sprite.spriteFrame = null;
                folds.push(sprite);
            }

            this._folds[side] = folds;

            var opt = sideChild.getChildByName("opt");
            opt.active = false;
            var sprite = opt.getChildByName("pai").getComponent(cc.Sprite);
            var data = {
                node: opt,
                sprite: sprite
            };
            this._opts.push(data);
        }
        var opts = gameChild.getChildByName("ops");
        this._options = opts;

        if (cc.vv.gameNetMgr.conf) {
            var type = cc.vv.gameNetMgr.conf.type;
            if (type == null || type == "") {
                type = "mj";
            }
        }

        if (cc.vv.replayMgr.isReplay()) {
            this.onGameBeign();
            this._zhishaibtn.active = false;
        }

        this.hideOptions();
        this.hideChupai();
        this.hideAllFolds();
    },

    hideChupai: function hideChupai() {
        for (var i = 0; i < this._chupaiSprite.length; ++i) {
            this._chupaiSprite[i].node.active = false;
        }
    },

    initEventHandlers: function initEventHandlers() {
        cc.vv.gameNetMgr.dataEventHandler = this.node;
        //初始化事件监听器
        var self = this;
        this.node.on('game_holds', function (data) {
            console.log("game_holds");
        });
        this.node.on('game_zhishai_ready', function (data) {
            console.log("game_zhishai_ready");
            if (cc.vv.userMgr.userId == data.detail) {
                self._btnKZ.active = true;
            }
        });
        this.node.on('game_zhishai', function (data) {
            console.log("game_zhishai");
            var zhishaiNum = 10;
            var dataJson = data.detail;
            var zhishaiAnimation = self._zhishaibtn.getComponent(cc.Animation);
            zhishaiAnimation.play('zhishai');
            (0, _timers.setTimeout)(function () {
                zhishaiAnimation.stop('zhishai');
                self._zhishaibtn.active = false;
            }, 1500);
            cc.vv.audioMgr.playSFX("Snd_Dice.mp3");
            cc.vv.common.initSeats();
        });

        this.node.on('game_begin', function (data) {
            console.log("game_begin");
            self._btnKZ.active = false;
            self.onGameBeign();
            self.initAllFolds();
            cc.vv.common.initSeats();
        });

        this.node.on('game_useful', function (data) {
            console.log('game_useful');
            var bao = cc.find("Canvas/arrow/useful/useful_pai");
            var baopai = bao.getComponent(cc.Sprite);
            var useful = cc.vv.gameNetMgr.useful;
            if (useful == 27) {
                useful = 30;
            } else if (useful == 31) {
                useful = 33;
            } else {
                useful = useful % 9 == 0 ? useful + 8 : useful - 1;
            }
            baopai.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("B_", useful);
        });

        this.node.on('game_sync', function (data) {
            self.onGameBeign();
            (0, _timers.setTimeout)(function () {
                if (!cc.vv.gameNetMgr.gameState) {
                    if (cc.vv.gameNetMgr.seatIndex == cc.vv.gameNetMgr.button) {
                        self._btnKZ.active = true;
                        self._zhishaibtn.active = true;
                    }
                } else {
                    self._zhishaibtn.active = false;
                    self.initAllFolds();
                    self.initMahjongs();
                }
            }, 200);
        });

        this.node.on('game_chupai', function (data) {
            data = data.detail;
            self.hideChupai();
            if (data.last != cc.vv.gameNetMgr.seatIndex) {
                self.initMopai(data.last, null);
            }
            if (!cc.vv.replayMgr.isReplay() && data.turn != cc.vv.gameNetMgr.seatIndex) {
                self.initMopai(data.turn, -1);
            }
        });

        this.node.on('game_mopai', function (data) {
            self.hideChupai();
            data = data.detail;
            var pai = data.pai;
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(data.seatIndex);
            if (localIndex == 0) {
                var index = 13;
                var sprite = self._myMJArr[index];
                if (pai == cc.vv.gameNetMgr.useful) {
                    sprite.node.color = cc.color(197, 197, 197);
                } else {
                    sprite.node.color = cc.color(255, 255, 255);
                }
                self.setSpriteFrameByMJID("M_", sprite, pai, index);
                sprite.node.mjId = pai;
            } else if (cc.vv.replayMgr.isReplay()) {
                self.initMopai(data.seatIndex, pai);
            }
        });

        this.node.on('game_action', function (data) {
            console.log("game_action");
            self.showAction(data.detail);
        });

        this.node.on('hupai', function (data) {
            var data = data.detail;
            //如果不是玩家自己，则将玩家的牌都放倒
            var seatIndex = data.seatindex;
            var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
            if (localIndex == 0) {
                self.hideOptions();
            }
            var seatData = cc.vv.gameNetMgr.seats[seatIndex];
            seatData.hued = true;
            var hupai = self._hupaiTips[localIndex];
            hupai.active = true;
            hupai.getChildByName("sprHu").active = !data.iszimo;
            hupai.getChildByName("sprZimo").active = data.iszimo;
            if (data.iszimo) {
                if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                    seatData.holds.pop();
                    self.initMahjongs();
                } else {
                    self.initOtherMahjongs(seatData);
                }
                self.playEfx(localIndex, "play_zimo");
            } else {
                if (!(data.iszimo && localIndex == 0)) {
                    if (cc.vv.replayMgr.isReplay() == false && localIndex != 0) {
                        self.initEmptySprites(seatIndex);
                    }
                    self.initMopai(seatIndex, data.hupai);
                }
                self.playEfx(localIndex, "play_hu");
            }

            if (cc.vv.replayMgr.isReplay() == true) {
                var opt = self._opts[localIndex];
                opt.node.active = true;
                opt.sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_", data.hupai);
            }
            var isnn = cc.vv.common.onSex(seatIndex);
            cc.vv.audioMgr.playSFX(isnn + "/mj/mj_hu.mp3");
        });

        this.node.on('mj_count', function (data) {
            self._mjcount.string = "剩余" + cc.vv.gameNetMgr.numOfMJ + "张";
        });

        this.node.on('game_over', function (data) {
            console.log('game_over');
            var dataOver = data.detail.results;
            var huType = cc.find("Canvas/arrow/huType");
            huType.active = true;
            var huTypeStr = huType.getComponent(cc.Label);
            var mj = {
                'ping hu': '平胡',
                'shi san yao': '十三幺',
                'peng peng hu': '碰碰胡',
                'qi zi quan': '七字全',
                'shi san lan': '十三烂',
                'qi dui': '七对',
                'qing yi se': '清一色',
                'hun yi se': '混一色',
                'zi yi se': '字一色',
                'gang hu': '杠胡',
                'tian hu': '天胡',
                'di hu': '地胡'
            };
            for (var i = 0; i < dataOver.length; ++i) {
                self.initOtherMahjongs(dataOver[i]);
                if (!dataOver[i].liuju) {
                    for (var j = 0; j < dataOver[i].actions.length; ++j) {
                        if (dataOver[i].actions[j].type == "hu" || dataOver[i].actions[j].type == "zimo") {
                            var hu = '';
                            var zx = cc.vv.gameNetMgr.button == dataOver[i].seatindex ? "庄" : "闲";
                            // console.log(dataOver[i].pattern.replace(/\s/g, ""))
                            for (var m in mj) {
                                if (m == dataOver[i].pattern) {
                                    hu = mj[m];
                                }
                            }
                            var bao = dataOver[i].fan == 0 ? "无宝" : "有宝";
                            var havebao = dataOver[i].baolen == 0 ? "" : "打" + dataOver[i].baolen + "宝";
                            huTypeStr.string = zx + '  ' + hu + '  ' + bao + '  ' + havebao;
                        }
                    }
                } else {
                    //这里是流局的操作
                    huTypeStr.string = '流局';
                    break;
                }
            }

            var gameChild = self.node.getChildByName('game');
            var gameChilddren = gameChild.children;
            for (var i = 0; i < gameChilddren.length; i++) {
                var cur = gameChilddren[i].getChildByName('cur');
                if (cur) {
                    cur.active = false;
                }
            }
        });

        this.node.on('game_chupai_notify', function (data) {
            self.hideChupai();
            var seatData = data.detail.seatData;
            //如果是自己，则刷新手牌
            if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                self.initMahjongs();
            } else {
                self.initOtherMahjongs(seatData);
            }
            self.initFolds(data.detail);
            self.showChupai();
            var audioUrl = cc.vv.mahjongmgr.getAudioURLByMJID(data.detail.pai, seatData.seatindex);
            cc.vv.audioMgr.playSFX(audioUrl);
        });

        this.node.on('guo_notify', function (data) {
            self.initFolds(data.detail);
            self.hideChupai();
            self.hideOptions();
            self._chisArr = [];
            var seatData = data.detail;
            //如果是自己，则刷新手牌
            if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                self.initMahjongs();
            }
            cc.vv.audioMgr.playSFX("give.mp3");
        });

        this.node.on('guo_result', function (data) {
            self.hideOptions();
        });

        this.node.on('peng_notify', function (data) {
            self.hideChupai();
            var seatData = data.detail;
            if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                self.initMahjongs();
            } else {
                self.initOtherMahjongs(seatData);
            }
            var localIndex = self.getLocalIndex(seatData.seatindex);
            self.playEfx(localIndex, "play_peng");
            var isnn = cc.vv.common.onSex(seatData.seatindex);
            cc.vv.audioMgr.playSFX(isnn + "/mj/mj_peng.mp3");
            self.hideOptions();
        });

        this.node.on('chi_notify', function (data) {
            self.hideChupai();
            var seatData = data.detail;
            if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                self.initMahjongs();
            } else {
                self.initOtherMahjongs(seatData);
            }
            var localIndex = self.getLocalIndex(seatData.seatindex);
            self.playEfx(localIndex, "play_chi");
            var isnn = cc.vv.common.onSex(seatData.seatindex);
            cc.vv.audioMgr.playSFX(isnn + "/mj/mj_chi.mp3");
            self.hideOptions();
        });

        this.node.on('gang_notify', function (data) {
            self.hideChupai();
            var data = data.detail;
            var seatData = data.seatData;
            var gangtype = data.gangtype;
            if (seatData.seatindex == cc.vv.gameNetMgr.seatIndex) {
                self.initMahjongs();
            } else {
                self.initOtherMahjongs(seatData);
            }

            var localIndex = self.getLocalIndex(seatData.seatindex);
        });

        this.node.on("hangang_notify", function (data) {
            var data = data.detail;
            var localIndex = self.getLocalIndex(data);
            self.playEfx(localIndex, "play_gang");
            var isnn = cc.vv.common.onSex(localIndex);
            cc.vv.audioMgr.playSFX(isnn + "/mj/mj_gang.mp3");
            self.hideOptions();
        });
    },

    showChupai: function showChupai() {
        var pai = cc.vv.gameNetMgr.chupai;
        if (pai >= 0) {
            var localIndex = this.getLocalIndex(cc.vv.gameNetMgr.turn);
            var sprite = this._chupaiSprite[localIndex];
            sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_", pai);
            sprite.node.active = true;
        }
    },

    hideAllFolds: function hideAllFolds() {
        for (var k in this._folds) {
            var f = this._folds[i];
            for (var i in f) {
                f[i].node.active = false;
            }
        }
    },

    onBtnZhishaiClicked: function onBtnZhishaiClicked() {
        cc.vv.net.send("zhishai", Math.ceil(Math.random() * 6));
    },

    addOption: function addOption(btnName, pai, chis) {
        for (var i = 0; i < this._options.childrenCount; ++i) {
            var child = this._options.children[i];
            if (child.name == "op" && child.active == false) {
                child.active = true;
                var btn = child.getChildByName(btnName);
                btn.active = true;
                btn.pai = pai;

                if (btnName == "btnChi") {
                    var children = btn.children;
                    for (var c = 0; c < chis.length; ++c) {
                        children[c].active = true;
                        var sprites = children[c].getComponentsInChildren(cc.Sprite);
                        for (var j = 0; j < chis[c].length; ++j) {
                            sprites[j].spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID("M_", chis[c][j]);
                        }
                    }
                }
                break;
            }
        }
    },

    hideOptions: function hideOptions(data) {
        this._options.active = false;
        for (var i = 0; i < this._options.childrenCount; ++i) {
            var child = this._options.children[i];
            if (child.name == "op") {
                child.active = false;
                child.getChildByName("btnChi").active = false;
                child.getChildByName("btnChi").children[0].active = false;
                child.getChildByName("btnChi").children[1].active = false;
                child.getChildByName("btnChi").children[2].active = false;

                child.getChildByName("btnPeng").active = false;
                child.getChildByName("btnGang").active = false;
                child.getChildByName("btnHu").active = false;
            }
        }
    },

    showAction: function showAction(data) {
        if (this._options.active) {
            this.hideOptions();
        }

        if (data && (data.hu || data.gang || data.peng || data.chi)) {
            this._options.active = true;
            if (data.hu) {
                this.addOption("btnHu", data.pai);
            }
            if (data.peng) {
                this.addOption("btnPeng", data.pai);
            }
            if (data.chi && data.chis != []) {
                this._chisArr = data.chisArr;
                this.addOption("btnChi", data.pai, data.chisArr);
            }
            if (data.gang) {
                for (var i = 0; i < data.gangpai.length; ++i) {
                    var gp = data.gangpai[i];
                    this.addOption("btnGang", gp);
                }
            }
        }
    },

    initHupai: function initHupai(localIndex, pai) {
        if (cc.vv.gameNetMgr.conf.type == "mj") {
            var hupailist = this._hupaiLists[localIndex];
            for (var i = 0; i < hupailist.children.length; ++i) {
                var hupainode = hupailist.children[i];
                if (hupainode.active == false) {
                    var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
                    hupainode.getComponent(cc.Sprite).spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, pai);
                    hupainode.active = true;
                    break;
                }
            }
        }
    },

    playEfx: function playEfx(index, name) {
        this._playEfxs[index].node.active = true;
        this._playEfxs[index].play(name);
    },

    onGameBeign: function onGameBeign() {
        for (var i = 0; i < this._playEfxs.length; ++i) {
            this._playEfxs[i].node.active = false;
        }
        var gameChild = this.node.getChildByName("game");
        var sides = ["myself", "right", "up", "left"];
        for (var i = 0; i < cc.vv.gameNetMgr.seats.length; ++i) {
            var seatData = cc.vv.gameNetMgr.seats[i];
            var holdsbool = false;
            for (var c in seatData) {
                if (c == 'holds') {
                    if (seatData[c] && seatData[c].length > 0) {
                        holdsbool = true;
                    }
                }
            }
            if (seatData.userId != 0 && seatData.name != "") {
                var localIndex = cc.vv.gameNetMgr.getLocalIndex(i);
                var sideChild = gameChild.getChildByName(sides[i]);
                var holds = sideChild.getChildByName("holds");

                sideChild.active = true;
                holds.active = true;
                for (var j = 0; j < holds.childrenCount; ++j) {
                    var nc = holds.children[j];
                    nc.active = true;
                    nc.scaleX = 1.0;
                    nc.scaleY = 1.0;
                    var sprite = nc.getComponent(cc.Sprite);
                    sprite.spriteFrame = cc.vv.mahjongmgr.holdsEmpty[i];
                }
                var hupai = this._hupaiTips[localIndex];
                hupai.active = seatData.hued;
                if (seatData.hued) {
                    hupai.getChildByName("sprHu").active = !seatData.iszimo;
                    hupai.getChildByName("sprZimo").active = seatData.iszimo;
                }
            }
        }

        this.hideChupai();
        this.hideOptions();

        var newSeats = [];
        for (var i = cc.vv.gameNetMgr.seatIndex; i < cc.vv.gameNetMgr.seats.length + cc.vv.gameNetMgr.seatIndex; ++i) {
            newSeats.push(cc.vv.gameNetMgr.seats[i % 4]);
        }
        this.gameRoot.active = true;
        var gameChild = this.node.getChildByName("game");
        for (var i = 1; i < sides.length; ++i) {
            var sideChild = gameChild.getChildByName(sides[i]);
            var holds = sideChild.getChildByName("holds");
            var holdsbool = false;
            for (var c in newSeats[i]) {
                if (c == 'holds' && cc.vv.replayMgr.isReplay()) {
                    holdsbool = newSeats[i][c] && newSeats[i][c].length > 0;
                }

                if (c == 'folds' && !cc.vv.replayMgr.isReplay()) {
                    holdsbool = true;
                }
            }
            if (newSeats[i].userid != 0 && newSeats[i].name != "" && holdsbool) {
                sideChild.active = true;
                holds.active = true;
                for (var j = 0; j < holds.childrenCount; ++j) {
                    var nc = holds.children[j];
                    nc.active = true;
                    nc.scaleX = 1.0;
                    nc.scaleY = 1.0;
                    var sprite = nc.getComponent(cc.Sprite);
                    sprite.spriteFrame = cc.vv.mahjongmgr.holdsEmpty[i];
                }
            } else {
                sideChild.active = false;
                holds.active = false;
            }
        }

        if (cc.vv.gameNetMgr.gamestate == "" && cc.vv.replayMgr.isReplay() == false) {
            return;
        }

        this.initMahjongs();
        var seats = cc.vv.gameNetMgr.seats;
        for (var i in seats) {
            var seatData = seats[i];
            if (seatData.userid != 0 && seatData.ready != false) {
                var localIndex = cc.vv.gameNetMgr.getLocalIndex(i);
                if (localIndex != 0) {
                    this.initOtherMahjongs(seatData);
                    if (i == cc.vv.gameNetMgr.turn) {
                        this.initMopai(i, -1);
                    } else {
                        this.initMopai(i, null);
                    }
                }
            }
        }

        this.showChupai();

        if (cc.vv.gameNetMgr.curaction != null) {
            this.showAction(cc.vv.gameNetMgr.curaction);
            cc.vv.gameNetMgr.curaction = null;
        }
    },

    onMJClicked: function onMJClicked(event) {
        //如果不是自己的轮子，则忽略
        if (cc.vv.gameNetMgr.turn != cc.vv.gameNetMgr.seatIndex) {
            console.log("not your turn." + cc.vv.gameNetMgr.turn);
            return;
        }

        for (var i = 0; i < this._myMJArr.length; ++i) {
            if (event.target == this._myMJArr[i].node) {
                //如果是再次点击，则出牌
                if (event.target == this._selectedMJ) {
                    this.shoot(this._selectedMJ.mjId);
                    this._selectedMJ.y = 0;
                    this._selectedMJ = null;
                    return;
                }
                if (this._selectedMJ != null) {
                    this._selectedMJ.y = 0;
                }
                event.target.y = 15;
                this._selectedMJ = event.target;
                return;
            }
        }
    },

    //出牌
    shoot: function shoot(mjId) {
        if (mjId == null) {
            return;
        }
        cc.vv.net.send('chupai', mjId);
    },

    getMJIndex: function getMJIndex(side, index) {
        if (side == "right" || side == "up") {
            return 13 - index;
        }
        return index;
    },

    initMopai: function initMopai(seatIndex, pai) {
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        var gameChild = this.node.getChildByName("game");
        var sideChild = gameChild.getChildByName(side);
        var holds = sideChild.getChildByName("holds");

        var lastIndex = this.getMJIndex(side, 13);
        var nc = holds.children[lastIndex];

        nc.scaleX = 1.0;
        nc.scaleY = 1.0;

        if (pai == null) {
            nc.active = false;
        } else if (pai >= 0) {
            nc.active = true;
            if (side == "up") {
                nc.scaleX = 0.73;
                nc.scaleY = 0.73;
            }
            var sprite = nc.getComponent(cc.Sprite);
            sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, pai);
        } else if (pai != null) {
            nc.active = true;
            if (side == "up") {
                nc.scaleX = 1.0;
                nc.scaleY = 1.0;
            }
            var sprite = nc.getComponent(cc.Sprite);
            sprite.spriteFrame = cc.vv.mahjongmgr.getHoldsEmptySpriteFrame(side);
        }
    },

    initAllFolds: function initAllFolds() {
        var seats = cc.vv.gameNetMgr.seats;
        for (var i in seats) {
            this.initFolds(seats[i]);
        }
    },

    initFolds: function initFolds(seatData) {
        var folds = seatData.folds;
        if (folds == null) {
            return;
        }
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatData.seatindex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var gameChild = this.node.getChildByName("game");
        if (folds.length > 0) {
            for (var i = 0; i < gameChild.children.length; i++) {
                var cur = gameChild.children[i].getChildByName('cur');
                if (cur) {
                    if (gameChild.children[i].name == side) {
                        cur.active = true;
                        var curAnimation = cur.getComponent(cc.Animation);
                        var foldscur = gameChild.children[i].getChildByName('folds');
                        var foldschil = side == 'right' || side == 'myself' ? foldscur.children[39 - folds.length] : foldscur.children[folds.length - 1];
                        curAnimation.play('curmahjong');
                        // console.log(foldscur.x)
                        var curx = foldscur.scaleX * foldschil.x + foldscur.x;
                        var cury = foldscur.scaleY * foldschil.y + foldscur.y;
                        cur.x = curx;
                        cur.y = cury + 20;
                    } else {
                        cur.active = false;
                    }
                }
            }
        }

        var foldsSprites = this._folds[side];
        for (var i = 0; i < foldsSprites.length; ++i) {
            var index = i;
            if (side == "right" || side == "myself") {
                index = foldsSprites.length - i - 1;
            }
            var sprite = foldsSprites[index];
            sprite.node.active = true;
            this.setSpriteFrameByMJID(pre, sprite, folds[i]);
            if (folds[i] == cc.vv.gameNetMgr.useful) {
                sprite.node.color = cc.color(197, 197, 197);
            } else {
                sprite.node.color = cc.color(255, 255, 255);
            }
        }

        for (var i = folds.length; i < foldsSprites.length; ++i) {
            var index = i;
            if (side == "right" || side == "myself") {
                index = foldsSprites.length - i - 1;
            }
            var sprite = foldsSprites[index];

            sprite.spriteFrame = null;
            sprite.node.active = false;
        }
    },

    initEmptySprites: function initEmptySprites(seatIndex) {
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);

        var gameChild = this.node.getChildByName("game");
        var sideChild = gameChild.getChildByName(side);
        var holds = sideChild.getChildByName("holds");
        var spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
        for (var i = 0; i < holds.childrenCount; ++i) {
            var nc = holds.children[i];
            nc.scaleX = 1.0;
            nc.scaleY = 1.0;

            var sprite = nc.getComponent(cc.Sprite);
            sprite.spriteFrame = spriteFrame;
        }
    },

    initOtherMahjongs: function initOtherMahjongs(seatData) {
        var localIndex = this.getLocalIndex(seatData.seatindex);
        if (localIndex == 0) {
            return;
        }
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var game = this.node.getChildByName("game");
        var sideRoot = game.getChildByName(side);
        var pengangroot = sideRoot.getChildByName("penggangs");
        var sideHolds = sideRoot.getChildByName("holds");
        var num = seatData.pengs.length + seatData.chis.length + seatData.angangs.length + seatData.diangangs.length + seatData.wangangs.length;
        num *= 3;
        for (var i = 0; i < num; ++i) {
            var idx = this.getMJIndex(side, i);
            sideHolds.children[idx].active = false;
        }

        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        var holds = this.sortHolds(seatData);
        if (holds != null && holds.length > 0) {
            for (var i = 0; i < holds.length; ++i) {
                var idx = this.getMJIndex(side, i + num);
                var sprite = sideHolds.children[idx].getComponent(cc.Sprite);
                if (side == "up") {
                    sprite.node.scaleX = 0.73;
                    sprite.node.scaleY = 0.73;
                }
                sprite.node.active = true;
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, holds[i]);
            }

            if (holds.length + num == 13) {
                var lasetIdx = this.getMJIndex(side, 13);
                sideHolds.children[lasetIdx].active = false;
            }
        }
    },

    sortHolds: function sortHolds(seatData) {
        var holds = seatData.holds;
        if (holds == null) {
            return null;
        }
        //如果手上的牌的数目是2,5,8,11,14，表示最后一张牌是刚摸到的牌
        var mopai = null;
        var l = holds.length;
        if (l == 2 || l == 5 || l == 8 || l == 11 || l == 14) {
            mopai = holds.pop();
        }

        cc.vv.mahjongmgr.sortMJ(holds);

        //将摸牌添加到最后
        if (mopai != null) {
            holds.push(mopai);
        }
        return holds;
    },

    initMahjongs: function initMahjongs() {
        var seats = cc.vv.gameNetMgr.seats;
        var seatData = seats[cc.vv.gameNetMgr.seatIndex];
        var holds = this.sortHolds(seatData);
        if (holds == null) {
            return;
        }

        //初始化手牌
        var lackingNum = (seatData.pengs.length + seatData.chis.length + seatData.angangs.length + seatData.diangangs.length + seatData.wangangs.length) * 3;
        for (var i = 0; i < holds.length; ++i) {
            var mjid = holds[i];
            var sprite = this._myMJArr[i + lackingNum];
            sprite.node.mjId = mjid;
            sprite.node.y = 0;
            this.setSpriteFrameByMJID("M_", sprite, mjid);
            if (mjid == cc.vv.gameNetMgr.useful) {
                sprite.node.color = cc.color(197, 197, 197);
            } else {
                sprite.node.color = cc.color(255, 255, 255);
            }
        }
        for (var i = 0; i < lackingNum; ++i) {
            var sprite = this._myMJArr[i];
            sprite.node.mjId = null;
            sprite.spriteFrame = null;
            sprite.node.active = false;
        }

        for (var i = lackingNum + holds.length; i < this._myMJArr.length; ++i) {
            var sprite = this._myMJArr[i];
            sprite.node.mjId = null;
            sprite.spriteFrame = null;
            sprite.node.active = false;
        }
    },

    setSpriteFrameByMJID: function setSpriteFrameByMJID(pre, sprite, mjid) {
        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
        sprite.node.active = true;
    },

    getLocalIndex: function getLocalIndex(index) {
        var ret = (index - cc.vv.gameNetMgr.seatIndex + 4) % 4;
        return ret;
    },

    onOptionClicked: function onOptionClicked(event) {
        if (event.target.name == "btnPeng") {
            cc.vv.net.send("peng");
        } else if (event.target.name == "btnGang") {
            cc.vv.net.send("gang", event.target.pai);
        } else if (event.target.name == "btnChiArr") {
            var eChildren = event.target.children;
            var num = parseInt(eChildren[0].name.substring(eChildren[0].name.length - 1, eChildren[0].name.length));
            // for (var i = 0; i < eChildren.length; i++) {
            //     var chiname = eChildren[i].getComponent(cc.Sprite).spriteFrame.name;
            //     var chinum = chiname.substring(chiname.length - 2)
            //     if (chinum.indexOf('_') != -1) {
            //         chinum = chinum.substring(1)
            //     }
            //     chiarr.push(parseInt(chinum))
            // }
            cc.vv.net.send("chi", this._chisArr[num]);
            this._chisArr = [];
        } else if (event.target.name == "btnHu") {
            cc.vv.net.send("hu");
        } else if (event.target.name == "btnGuo") {
            cc.vv.net.send("guo");
        }
    }
});

cc._RF.pop();